#include "stdafx.h"
#include "ModuleVersionFileListWindow.h"

CModuleVersionFileListWindow::CModuleVersionFileListWindow()
{
}

CModuleVersionFileListWindow::~CModuleVersionFileListWindow()
{
}

#define INSERT_COLUMN( iSubItem2, text, width, format)	\
	ZeroMemory( &column, sizeof( LVCOLUMN));\
	column.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;\
	column.fmt = format;\
	column.iSubItem = iSubItem2;\
	column.cx = width;\
	column.pszText = (char*)text;\
	column.cchTextMax = sizeof( text);\
	ListView_InsertColumn( m_hWnd, iSubItem2, &column);

bool CModuleVersionFileListWindow::Create(HINSTANCE hInstance,HWND hParent,UINT nChildID)
{
	RECT rcWindow;
	ZeroMemory( &rcWindow, sizeof( RECT));
	rcWindow.left = 0;
	rcWindow.right = 100;
	rcWindow.top = 0;
	rcWindow.bottom = 100;


	m_hWnd =::CreateWindowEx( WS_EX_CLIENTEDGE, "SysListView32", NULL, WS_VISIBLE | WS_CHILD | LVS_REPORT, rcWindow.left, rcWindow.top, RECT_WIDTH( rcWindow), RECT_HEIGHT( rcWindow), hParent, (HMENU)nChildID, hInstance, NULL);
	if( m_hWnd == NULL)
		return false;

	ListView_SetExtendedListViewStyle( m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_FLATSB | LVS_EX_HEADERDRAGDROP);

	LVCOLUMN column;
	INSERT_COLUMN( 0, "Name", 150, LVCFMT_LEFT);
	INSERT_COLUMN( 1, "Path", 250, LVCFMT_LEFT);
	INSERT_COLUMN( 2, "Size", 100, LVCFMT_LEFT);
	INSERT_COLUMN( 3, "Time", 150, LVCFMT_LEFT);
	INSERT_COLUMN( 4, "Pack", 60, LVCFMT_LEFT);

	return true;
}

void CModuleVersionFileListWindow::Release()
{
}

void CModuleVersionFileListWindow::InsertFile(sModuleVersionFile* pFile)
{
	static LVITEM item;
	static SYSTEMTIME time;
	static char buff[ 255];

	ZeroMemory( &item, sizeof( LVITEM));
	item.mask = LVIF_TEXT | LVIF_PARAM;
	item.pszText = (char*)pFile->m_szFilename;
	item.cchTextMax = sizeof( pFile->m_szFilename);
	item.lParam = (DWORD)pFile;
	item.iItem = ListView_GetItemCount( m_hWnd);

	int iIndex = ListView_InsertItem( m_hWnd, &item);

	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 0;
	item.pszText = (char*)pFile->m_szFilename;
	item.cchTextMax = sizeof( pFile->m_szFilename);
	ListView_SetItem( m_hWnd, &item);

	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 1;
	item.pszText = (char*)pFile->m_szPath;
	item.cchTextMax = sizeof( pFile->m_szPath);
	ListView_SetItem( m_hWnd, &item);

	sprintf_s( buff, 255, "%d", pFile->m_nFileSize);

	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 2;
	item.pszText = (char*)buff;
	item.cchTextMax = sizeof( buff);
	ListView_SetItem( m_hWnd, &item);

	sprintf_s( buff, 255, "%04d/%02d/%02d - %02d:%02d:%02d", 
		pFile->m_timeModified.m_nYear, pFile->m_timeModified.m_nMonth, pFile->m_timeModified.m_nDay, pFile->m_timeModified.m_nHour, pFile->m_timeModified.m_nMinute, pFile->m_timeModified.m_nSecond);
	
	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 3;
	item.pszText = (char*)buff;
	item.cchTextMax = sizeof( item.pszText);
	ListView_SetItem( m_hWnd, &item);

	sprintf_s( buff, 255, "%s", pFile->m_nToBePacked ? "TRUE" : "FALSE");

	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 4;
	item.pszText = (char*)buff;
	item.cchTextMax = sizeof( buff);
	ListView_SetItem( m_hWnd, &item);
}

void CModuleVersionFileListWindow::Clear()
{
	ListView_DeleteAllItems( m_hWnd);
}